<?php
namespace Common\Util;

/**
 * 媒体文件
 */
class FFMpeg
{
    public $ffmpeg;

    public function __construct(){
        $this->ffmpeg = \FFMpeg\FFMpeg::create([
            'ffmpeg.binaries'  => 'ffmpeg',
            'ffprobe.binaries' => 'ffprobe',
        ]);
        $this->ffprobe = \FFMpeg\FFProbe::create();
    }

    /**
     * 给视频添加音频
     * @param    string     $video 视频地址
     * @param    string     $autio 音频地址
     * @param    string     $path  合成文件的路径
     * @return   array
     */
    public function video_concat_audio($video, $autio, $path)
    {
        try{
            $driver = $this->ffmpeg->getFFMpegDriver();
            $commands = [];
            $commands[] = '-i';
            $commands[] = $video;
            $commands[] = '-i';
            $commands[] = $autio;
            // $commands[] = '-c';
            // $commands[] = 'copy';
            $commands[] = '-vcodec';
            $commands[] = 'copy';
            // $commands[] = '-codec';
            // $commands[] = 'copy';
            $commands[] = '-shortest';
            $commands[] = $path;
            $driver->command($commands);
            return ['status'=>1, 'msg'=>'ok'];
        }catch(\Exception $e){
            return ['status'=>0, 'msg'=>$e->getMessage()];
        }
    }

    public function command($commands)
    {
        try{
            $driver = $this->ffmpeg->getFFMpegDriver();
            $driver->command($commands);
            return ['status'=>1, 'msg'=>'ok'];
        }catch(\Exception $e){
            return ['status'=>0, 'msg'=>$e->getMessage()];
        }
    }

    /**
     * 视频文件信息
     * @param    string     $path 路径
     * @return   array
     */
    public function video_info($path)
    {
        try{
            $video = $this->ffmpeg->open($path);
            if($video->getStreams()->videos()){
                $streams = $video->getStreams()->videos()->all();
                // dump($video->getStreams()->videos());
                if(!empty($streams[0])){
                    $stream = $streams[0];
                    $info = $stream->all();
                    return ['status'=>1, 'msg'=>'ok', 'info'=>$info, 'format'=>$video->getFormat()];
                }
            }
            return ['status'=>0, 'msg'=>'失败'];
        }catch(\Exception $e){
            return ['status'=>0, 'msg'=>$e->getMessage()];
        }
    }

    /**
     * 音频文件信息
     * @param    string     $path 路径
     * @return   array
     */
    public function audio_info($path)
    {
        try{
            $audio = $this->ffmpeg->open($path);
            if($audio->getStreams()->audios()){
                $streams = $audio->getStreams()->audios()->all();
                if(!empty($streams[0])){
                    $stream = $streams[0];
                    $info = $stream->all();
                    return ['status'=>1, 'msg'=>'ok', 'info'=>$info];
                }
            }
            return ['status'=>0, 'msg'=>'失败'];
        }catch(\Exception $e){
            return ['status'=>0, 'msg'=>$e->getMessage()];
        }
    }

    /**
     * 媒体文件信息
     * @param    string     $path 路径
     * @return   array
     */
    public function media_info($path)
    {
        try{
            $media = $this->ffmpeg->open($path);
            if($media->getStreams()->all()){
                $streams = $media->getStreams()->all();
                $infos = [];
                foreach ($streams as $key => $stream) {
                    $infos[] = $stream->all();
                }
                return ['status'=>1, 'msg'=>'ok', 'infos'=>$infos];
            }
            return ['status'=>0, 'msg'=>'失败'];
        }catch(\Exception $e){
            return ['status'=>0, 'msg'=>$e->getMessage()];
        }
    }
}